package com.ruoyi.framework.aspectj;

import java.util.Collections;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.context.PermissionContextHolder;
import com.ruoyi.common.utils.StringUtils;

/**
 * 自定义权限拦截器，将权限字符串放到当前请求中以便用于多个角色匹配符合要求的权限
 * 
 * @author ruoyi
 */
@Aspect
@Component
public class PermissionsAspect
{
    @Before("@annotation(controllerRequiresPermissions)")
    public void doBefore(JoinPoint point, RequiresPermissions controllerRequiresPermissions) throws Throwable
    {
        handleRequiresPermissions(point, controllerRequiresPermissions);
    }

    protected void handleRequiresPermissions(final JoinPoint joinPoint, RequiresPermissions requiresPermissions)
    {
    	String permissKey=StringUtils.join(requiresPermissions.value(), ",");
    	//检验权限,如果无权限则会停止过滤器链路
    	SecurityUtils.getSecurityManager().checkPermission(SecurityUtils.getSubject().getPrincipals(),permissKey);
    	//判断是否有权限,返回true or false,手动决定后续操作.
    	//boolean[] result=SecurityUtils.getSubject().isPermitted(requiresPermissions.value());        
    	PermissionContextHolder.setContext(permissKey);
    }
}
